#include "infoform.h"
#include "ui_infoform.h"
InfoForm *InfoForm::myinfo=nullptr;

InfoForm::InfoForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::InfoForm)
{
    ui->setupUi(this);
    for(int i =0;i<100;i++)
    {
        chatbutton1.push_back(new QPushButton());

        chatbutton2.push_back(new QPushButton());
    }



    tableView = new QTableView(ui->tableView);
     tableView->setMinimumSize(700, 500);
     tableView->verticalHeader()->hide(); // hide row number
     tableModel = new QStandardItemModel(ui->tableView);
     tableView->setModel(tableModel);
}

InfoForm::~InfoForm()
{
    delete ui;
}
void InfoForm::getinfo(){

this->show();
    Account::getInstance()->getPost(infolist);



qDebug()<<infolist;
    // set columns
    QStringList columnTitles;
    columnTitles << "序号"<<"uid" << "gid" <<"接受"<<"拒绝";
    tableModel->setHorizontalHeaderLabels(columnTitles);
//    tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents); // column fit the contents

    // add contents

          QMap<QString,QString>::Iterator it = infolist.begin();
        int  num=0;
            while(it!=infolist.end()){
                qDebug()<<"num:"<<num;
                tableModel->setItem(num, 0, new QStandardItem(QString::number(num + 1)));
                tableModel->setItem(num, 1, new QStandardItem(QString( it.key()).arg(num)));
                tableModel->setItem(num, 2, new QStandardItem( it.value()));
                  tableModel->setItem(num, 3, new QStandardItem("申请"));
                 // add button to the last column

//                 QPushButton *chatbutton1 = new  QPushButton("接受");
                 chatbutton1[num]->setText("接受");
//                 QPushButton *chatbutton2 = new  QPushButton("拒绝");
                     chatbutton2[num]->setText("拒绝");
               // chatbutton->id= it.key();
                 // set custom property


                 chatbutton1[num]->setProperty("序号", num); // set custom property
                 chatbutton1[num]->setProperty("gid", it.key());
                 chatbutton1[num]->setProperty("uid",  it.value());
                 chatbutton2[num]->setProperty("序号", num); // set custom property
                 chatbutton2[num]->setProperty("gid", it.key());
                 chatbutton2[num]->setProperty("uid",  it.value());

                 // set click event
              connect(chatbutton1[num], SIGNAL(clicked()), this, SLOT(on_chatbutton_clicked1()));
               connect(chatbutton2[num], SIGNAL(clicked()), this, SLOT(on_chatbutton_clicked2()));

                 tableView->setIndexWidget(tableModel->index(tableModel->rowCount() - 1, 3), chatbutton1[num]);
                  tableView->setIndexWidget(tableModel->index(tableModel->rowCount() - 1, 4), chatbutton2[num]);
          num++;
                it++;
            }

}

void InfoForm::on_chatbutton_clicked1(){
               QPushButton *button=(QPushButton*)sender();

          emit(recovery(button->property("uid").toString(),button->property("gid").toString(),true));

      chatbutton1[button->property("序号").toInt()]->setEnabled(false);
        chatbutton2[button->property("序号").toInt()]->setEnabled(false);


          }

void InfoForm::on_chatbutton_clicked2(){
            QPushButton *button=(QPushButton*)sender();
      emit(recovery(button->property("uid").toString(),button->property("gid").toString(),false));
            chatbutton1[button->property("序号").toInt()]->setEnabled(false);
              chatbutton2[button->property("序号").toInt()]->setEnabled(false);

          }


